using MicroMsg.Common.Event;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace MicroMsg.Manager
{
	public class FMsgMgr
	{
		private const string TAG = "FMsgMgr";
		public const string FMsgUserName = "fmessage";
		private static List<FMsgChange> FMsgChangeObserverSet = new List<FMsgChange>();
		private static Dictionary<int, FMsgType> mapType;
		private static bool bVerifyAlready;
		public static void addObserver(FMsgChange observer)
		{
			if (!FMsgMgr.FMsgChangeObserverSet.Contains(observer))
			{
				FMsgMgr.FMsgChangeObserverSet.Add(observer);
			}
		}
		public static void removeObserver(FMsgChange observer)
		{
			if (FMsgMgr.FMsgChangeObserverSet.Contains(observer))
			{
				FMsgMgr.FMsgChangeObserverSet.Remove(observer);
			}
		}
		private static void FMsgChangeNotify(int notify_type, ChatMsg chatMsg_0, FMsgType type)
		{
			using (List<FMsgChange>.Enumerator enumerator = FMsgMgr.FMsgChangeObserverSet.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					FMsgChange current = enumerator.get_Current();
					switch (notify_type)
					{
					case 0:
						current.onFMsgAdd(chatMsg_0, type);
						break;
					case 1:
						current.onFMsgDel(chatMsg_0, type);
						break;
					case 2:
						current.onFMsgNewNumChange(type);
						break;
					}
				}
			}
		}
		public static void init()
		{
			ChatMsgMgr.registerCleanMsg(new Action(FMsgMgr.CleanAll));
			EventCenter.registerEventHandler(EventConst.ON_STORAGE_CHAT_MSG_DEL, new EventHandlerDelegate(FMsgMgr.onChatMsgDel));
		}
		public static void loadFmsg()
		{
			int msgNum = StorageMgr.chatMsg.getMsgNum("fmessage");
			if (msgNum <= 0)
			{
				return;
			}
			List<ChatMsg> msgList = StorageMgr.chatMsg.getMsgList("fmessage", 0, msgNum);
			using (List<ChatMsg>.Enumerator enumerator = msgList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ChatMsg current = enumerator.get_Current();
					FMsgMgr.addFMsg(current);
				}
			}
		}
		public static void CleanAll()
		{
			FMsgGroup fMsgGroup = ConfigMgr.smethod_0<FMsgGroup>();
			fMsgGroup.msgList.Clear();
			fMsgGroup.mapNewMsg.Clear();
			ConfigMgr.delayWrite<FMsgGroup>(fMsgGroup);
		}
		public static void CleanByType(FMsgType type)
		{
			FMsgGroup fMsgGroup = ConfigMgr.smethod_0<FMsgGroup>();
			fMsgGroup.mapNewMsg.set_Item((int)type, 0);
			List<FMsg> list = Enumerable.ToList<FMsg>(Enumerable.Where<FMsg>(fMsgGroup.msgList, (FMsg fmsg_0) => fmsg_0.nType == (int)type));
			if (list.get_Count() > 0)
			{
				fMsgGroup.msgList = Enumerable.ToList<FMsg>(Enumerable.Except<FMsg>(fMsgGroup.msgList, list));
				using (List<FMsg>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						FMsg current = enumerator.get_Current();
						StorageMgr.chatMsg.delMsg("fmessage", current.nMsgSvrID);
					}
				}
			}
			ConfigMgr.delayWrite<FMsgGroup>(fMsgGroup);
		}
		public static int getScene(ChatMsg chatMsg_0)
		{
			try
			{
				int result;
				if (chatMsg_0.strTalker != "fmessage")
				{
					result = -1;
					return result;
				}
				XDocument xDocument = XDocument.Parse(chatMsg_0.strMsg);
				if (xDocument == null)
				{
					result = -1;
					return result;
				}
				XElement xElement = xDocument.Element("msg");
				if (xElement == null)
				{
					result = -1;
					return result;
				}
				XAttribute xAttribute = xElement.Attribute("scene");
				if (xAttribute == null)
				{
					result = -1;
					return result;
				}
				result = int.Parse(xAttribute.get_Value());
				return result;
			}
			catch (Exception)
			{
			}
			return -1;
		}
		private static FMsgType getFMsgType(int scene)
		{
			if (FMsgMgr.mapType.ContainsKey(scene))
			{
				return FMsgMgr.mapType.get_Item(scene);
			}
			return FMsgType.other;
		}
		public static FMsgType getFMsgType(ChatMsg chatMsg_0)
		{
			int scene = FMsgMgr.getScene(chatMsg_0);
			return FMsgMgr.getFMsgType(scene);
		}
		public static void addFMsg(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null || chatMsg_0.strTalker != "fmessage")
			{
				return;
			}
			FMsgGroup fMsgGroup = ConfigMgr.smethod_0<FMsgGroup>();
			FMsg fMsg = Enumerable.FirstOrDefault<FMsg>(Enumerable.Where<FMsg>(fMsgGroup.msgList, (FMsg fmsg_0) => fmsg_0.nMsgSvrID == chatMsg_0.nMsgSvrID));
			if (fMsg != null)
			{
				return;
			}
			int scene = FMsgMgr.getScene(chatMsg_0);
			if (scene < 0)
			{
				return;
			}
			FMsgType fMsgType = FMsgMgr.getFMsgType(scene);
			int num = (int)fMsgType;
			if (!fMsgGroup.mapNewMsg.ContainsKey(num))
			{
				fMsgGroup.mapNewMsg.set_Item(num, 1);
			}
			else
			{
				Dictionary<int, int> mapNewMsg;
				int num2;
				(mapNewMsg = fMsgGroup.mapNewMsg).set_Item(num2 = num, mapNewMsg.get_Item(num2) + 1);
			}
			fMsgGroup.msgList.Insert(0, new FMsg(chatMsg_0.nMsgSvrID, num, (uint)chatMsg_0.nCreateTime));
			ConfigMgr.delayWrite<FMsgGroup>(fMsgGroup);
			FMsgMgr.FMsgChangeNotify(0, chatMsg_0, fMsgType);
		}
		private static void onChatMsgDel(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<ChatMsg> listObject = evtArgs.getListObject<ChatMsg>();
			if (listObject == null)
			{
				return;
			}
			FMsgGroup fMsgGroup = ConfigMgr.smethod_0<FMsgGroup>();
			using (List<ChatMsg>.Enumerator enumerator = listObject.GetEnumerator())
			{
				ChatMsg chatMsg_0;
				while (enumerator.MoveNext())
				{
					chatMsg_0 = enumerator.get_Current();
					if (!(chatMsg_0.strTalker != "fmessage"))
					{
						FMsg fMsg = Enumerable.FirstOrDefault<FMsg>(Enumerable.Where<FMsg>(fMsgGroup.msgList, (FMsg fmsg_0) => fmsg_0.nMsgSvrID == chatMsg_0.nMsgSvrID));
						if (fMsg != null)
						{
							fMsgGroup.msgList.Remove(fMsg);
							FMsgMgr.FMsgChangeNotify(1, chatMsg_0, FMsgMgr.getFMsgType(FMsgMgr.getScene(chatMsg_0)));
							ConfigMgr.delayWrite<FMsgGroup>(fMsgGroup);
						}
					}
				}
			}
		}
		public static int getNewMsgNum(FMsgType type)
		{
			FMsgGroup fMsgGroup = ConfigMgr.smethod_0<FMsgGroup>();
			if (!fMsgGroup.mapNewMsg.ContainsKey((int)type))
			{
				return 0;
			}
			return fMsgGroup.mapNewMsg.get_Item((int)type);
		}
		public static void setNewMsgNum(FMsgType type, int number)
		{
			FMsgGroup fMsgGroup = ConfigMgr.smethod_0<FMsgGroup>();
			fMsgGroup.mapNewMsg.set_Item((int)type, number);
			ConfigMgr.delayWrite<FMsgGroup>(fMsgGroup);
			FMsgMgr.FMsgChangeNotify(2, null, type);
		}
		public static int getMsgNum(FMsgType type)
		{
			FMsgGroup fMsgGroup = ConfigMgr.smethod_0<FMsgGroup>();
			return Enumerable.Count<FMsg>(Enumerable.Where<FMsg>(fMsgGroup.msgList, (FMsg fmsg_0) => fmsg_0.nType == (int)type));
		}
		public static List<ChatMsg> getMsgList(FMsgType type, int start, int int_0, bool bReverse = false)
		{
			FMsgGroup fMsgGroup = ConfigMgr.smethod_0<FMsgGroup>();
			List<FMsg> arg_8A_0;
			if (!bReverse)
			{
				arg_8A_0 = Enumerable.ToList<FMsg>(Enumerable.Take<FMsg>(Enumerable.Skip<FMsg>(Enumerable.Where<FMsg>(fMsgGroup.msgList, (FMsg fmsg_0) => fmsg_0.nType == (int)type), start), int_0));
			}
			else
			{
				arg_8A_0 = Enumerable.ToList<FMsg>(Enumerable.Take<FMsg>(Enumerable.Skip<FMsg>(Enumerable.OrderBy<FMsg, uint>(Enumerable.Where<FMsg>(fMsgGroup.msgList, (FMsg fmsg_0) => fmsg_0.nType == (int)type), (FMsg fmsg_0) => fmsg_0.nCreateTime), start), int_0));
			}
			List<FMsg> list = arg_8A_0;
			if (list != null && list.get_Count() > 0)
			{
				List<ChatMsg> list2 = new List<ChatMsg>();
				using (List<FMsg>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						FMsg current = enumerator.get_Current();
						ChatMsg bySvrID = StorageMgr.chatMsg.getBySvrID("fmessage", current.nMsgSvrID);
						if (bySvrID != null)
						{
							list2.Add(bySvrID);
							if (current.nCreateTime == 0u)
							{
								current.nCreateTime = (uint)bySvrID.nCreateTime;
							}
						}
					}
				}
				return list2;
			}
			return null;
		}
		public static void verify()
		{
			if (FMsgMgr.bVerifyAlready)
			{
				return;
			}
			FMsgMgr.bVerifyAlready = true;
			int msgNum = StorageMgr.chatMsg.getMsgNum("fmessage");
			List<ChatMsg> msgList = StorageMgr.chatMsg.getMsgList("fmessage", 0, msgNum);
			FMsgGroup fMsgGroup = ConfigMgr.smethod_0<FMsgGroup>();
			fMsgGroup.msgList.Clear();
			using (List<ChatMsg>.Enumerator enumerator = msgList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ChatMsg current = enumerator.get_Current();
					FMsgMgr.addFMsg(current);
				}
			}
			fMsgGroup.mapNewMsg.Clear();
			ConfigMgr.delayWrite<FMsgGroup>(fMsgGroup);
		}
		private static void dumpFMsg()
		{
			ConfigMgr.smethod_0<FMsgGroup>();
			Conversation conversation = StorageMgr.converation.method_0("fmessage");
			StorageMgr.chatMsg.getMsg(conversation.nMsgLocalID);
			int msgNum = StorageMgr.chatMsg.getMsgNum("fmessage");
			List<ChatMsg> msgList = StorageMgr.chatMsg.getMsgList("fmessage", 0, msgNum);
			using (List<ChatMsg>.Enumerator enumerator = msgList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ChatMsg arg_5A_0 = enumerator.get_Current();
				}
			}
		}
		static FMsgMgr()
		{
			// Note: this type is marked as 'beforefieldinit'.
			Dictionary<int, FMsgType> dictionary = new Dictionary<int, FMsgType>();
			dictionary.Add(18, FMsgType.const_0);
			dictionary.Add(26, FMsgType.shake);
			dictionary.Add(27, FMsgType.shake);
			dictionary.Add(28, FMsgType.shake);
			dictionary.Add(29, FMsgType.shake);
			dictionary.Add(22, FMsgType.shake);
			dictionary.Add(23, FMsgType.shake);
			dictionary.Add(24, FMsgType.shake);
			FMsgMgr.mapType = dictionary;
			FMsgMgr.bVerifyAlready = false;
		}
	}
}
